package com.srmt.document.business.quote;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 供应商报价数据模型
 *
 * <p>用于封装供应商在某一轮次的报价信息
 *
 * <h3>使用示例：</h3>
 * <pre>{@code
 * SupplierQuoteData quote = SupplierQuoteData.builder()
 *     .supplierName("A-供应商")
 *     .round(1)
 *     .addQuoteValue("100")
 *     .addQuoteValue("200")
 *     .addQuoteValue("300")
 *     .build();
 * }</pre>
 *
 * @author SRMT Development Team
 * @since 1.0.0
 */
public class SupplierQuoteData {

    /**
     * 供应商名称（如：A-供应商、B-供应商）
     */
    private String supplierName;

    /**
     * 报价轮次（如：1表示第一轮，2表示第二轮）
     */
    private int round;

    /**
     * 报价数据列表
     * <p>按从上往下的顺序排列，仅包含需要自动填写的单元格值
     * <p>不包含非自动填写的单元格（如手动输入的特殊字段）
     */
    private List<String> quoteValues;

    /**
     * 默认构造函数
     */
    public SupplierQuoteData() {
        this.quoteValues = new ArrayList<>();
    }

    /**
     * 全参构造函数
     *
     * @param supplierName 供应商名称
     * @param round 报价轮次
     * @param quoteValues 报价数据列表
     */
    public SupplierQuoteData(String supplierName, int round, List<String> quoteValues) {
        this.supplierName = supplierName;
        this.round = round;
        this.quoteValues = quoteValues != null ? new ArrayList<>(quoteValues) : new ArrayList<>();
    }

    // ============ Getter和Setter ============

    public String getSupplierName() {
        return supplierName;
    }

    public void setSupplierName(String supplierName) {
        this.supplierName = supplierName;
    }

    public int getRound() {
        return round;
    }

    public void setRound(int round) {
        this.round = round;
    }

    public List<String> getQuoteValues() {
        return quoteValues;
    }

    public void setQuoteValues(List<String> quoteValues) {
        this.quoteValues = quoteValues != null ? new ArrayList<>(quoteValues) : new ArrayList<>();
    }

    // ============ Builder模式 ============

    /**
     * 创建Builder
     *
     * @return Builder实例
     */
    public static Builder builder() {
        return new Builder();
    }

    /**
     * Builder类
     */
    public static class Builder {
        private String supplierName;
        private int round;
        private List<String> quoteValues = new ArrayList<>();

        public Builder supplierName(String supplierName) {
            this.supplierName = supplierName;
            return this;
        }

        public Builder round(int round) {
            this.round = round;
            return this;
        }

        public Builder quoteValues(List<String> quoteValues) {
            this.quoteValues = quoteValues != null ? new ArrayList<>(quoteValues) : new ArrayList<>();
            return this;
        }

        /**
         * 添加单个报价值
         *
         * @param value 报价值
         * @return Builder实例
         */
        public Builder addQuoteValue(String value) {
            this.quoteValues.add(value);
            return this;
        }

        /**
         * 批量添加报价值
         *
         * @param values 报价值数组
         * @return Builder实例
         */
        public Builder addQuoteValues(String... values) {
            for (String value : values) {
                this.quoteValues.add(value);
            }
            return this;
        }

        public SupplierQuoteData build() {
            if (supplierName == null || supplierName.trim().isEmpty()) {
                throw new IllegalArgumentException("供应商名称不能为空");
            }
            if (round <= 0) {
                throw new IllegalArgumentException("报价轮次必须大于0");
            }
            return new SupplierQuoteData(supplierName, round, quoteValues);
        }
    }

    // ============ 辅助方法 ============

    /**
     * 获取报价值数量
     *
     * @return 报价值数量
     */
    public int getQuoteValueCount() {
        return quoteValues != null ? quoteValues.size() : 0;
    }

    /**
     * 获取指定索引的报价值
     *
     * @param index 索引（从0开始）
     * @return 报价值，如果索引无效返回null
     */
    public String getQuoteValue(int index) {
        if (quoteValues != null && index >= 0 && index < quoteValues.size()) {
            return quoteValues.get(index);
        }
        return null;
    }

    /**
     * 添加报价值
     *
     * @param value 报价值
     */
    public void addQuoteValue(String value) {
        if (this.quoteValues == null) {
            this.quoteValues = new ArrayList<>();
        }
        this.quoteValues.add(value);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SupplierQuoteData that = (SupplierQuoteData) o;
        return round == that.round &&
               Objects.equals(supplierName, that.supplierName) &&
               Objects.equals(quoteValues, that.quoteValues);
    }

    @Override
    public int hashCode() {
        return Objects.hash(supplierName, round, quoteValues);
    }

    @Override
    public String toString() {
        return "SupplierQuoteData{" +
               "supplierName='" + supplierName + '\'' +
               ", round=" + round +
               ", quoteValues=" + quoteValues +
               '}';
    }
}
